package by.amakarevich.webapp.page.edit.tambour;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.Tambour;
import by.amakarevich.services.TambourService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.home.HomePage;


public class TambourCreateEditPage extends AlternativeBaseLayout {

	@Inject
	private TambourService tambourService;
	
	public TambourCreateEditPage(final Tambour tambour, final Product product) {
		super();
		Form<Tambour> form = new Form<Tambour>("form", new CompoundPropertyModel<Tambour>(tambour));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>());
		tfName.setLabel(new ResourceModel("p.product.name"));
		form.add(tfName);

		final TextField<Long> tfDiameter = new TextField<Long>("diameter");
		tfDiameter.add(new PropertyValidator<Long>());
		tfDiameter.setLabel(new ResourceModel("p.product.diameter"));
		form.add(tfDiameter);
		
		final TextField<Long> tfHeight = new TextField<Long>("height");
		tfHeight.add(new PropertyValidator<Long>());
		tfHeight.setLabel(new ResourceModel("p.product.height"));
		form.add(tfHeight);
		
		final TextField<Long> tfWidth = new TextField<Long>("width");
		tfWidth.add(new PropertyValidator<Long>());
		tfWidth.setLabel(new ResourceModel("p.product.width"));
		form.add(tfWidth);
		
		final TextField<String> tfMaterial = new TextField<String>("material");
		tfMaterial.add(new PropertyValidator<String>());
		tfMaterial.setLabel(new ResourceModel("p.product.material"));
		form.add(tfMaterial);
		
		form.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				if(tambour.getId()==null){
					tambourService.createNewTambour(tambour, product);
				}else{
					tambourService.updateTambour(tambour);
				}
					
				HomePage page = new HomePage();


				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}
		

}
